<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chuangges.gitee.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="栈、队列、链表、集合、字典、散列表、树、图">
<meta property="og:type" content="article">
<meta property="og:title" content="JS 数据结构">
<meta property="og:url" content="https://chuangges.gitee.io/2019/05/20/4-4-js%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/index.html">
<meta property="og:site_name" content="闯哥要去流浪了">
<meta property="og:description" content="栈、队列、链表、集合、字典、散列表、树、图">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://chuangges.gitee.io/images/js/link-list.png">
<meta property="og:image" content="https://chuangges.gitee.io/images/js/binary-order.png">
<meta property="og:image" content="https://chuangges.gitee.io/images/js/binary-link.png">
<meta property="article:published_time" content="2019-05-20T13:40:45.000Z">
<meta property="article:modified_time" content="2023-05-11T11:04:17.713Z">
<meta property="article:author" content="chuangges">
<meta property="article:tag" content="Javascript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://chuangges.gitee.io/images/js/link-list.png">

<link rel="canonical" href="https://chuangges.gitee.io/2019/05/20/4-4-js%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>JS 数据结构 | 闯哥要去流浪了</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="闯哥要去流浪了" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">闯哥要去流浪了</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">愿你岁月可回首，且以深情共白头</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chuangges.gitee.io/2019/05/20/4-4-js%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/chuang.png">
      <meta itemprop="name" content="chuangges">
      <meta itemprop="description" content="chuangge's blog.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="闯哥要去流浪了">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JS 数据结构
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-05-20 21:40:45" itemprop="dateCreated datePublished" datetime="2019-05-20T21:40:45+08:00">2019-05-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-05-11 19:04:17" itemprop="dateModified" datetime="2023-05-11T19:04:17+08:00">2023-05-11</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Javascript/" itemprop="url" rel="index"><span itemprop="name">Javascript</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>18k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>16 分钟</span>
            </span>
            <div class="post-description">栈、队列、链表、集合、字典、散列表、树、图</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <ul>
<li>逻辑结构：数据组织方式。<ul>
<li><strong>集合结构</strong>：由一组无序、唯一、无关联的元素组成，主要操作有并集、交集、补集。</li>
<li><strong>线性结构</strong>：堆、栈、队列、一维数组的线性表，元素是有序的并有一对一的层次关系。</li>
<li><strong>树形结构</strong>：树，元素之间存在一对多的层次关系。</li>
<li><strong>图形结构</strong>：图，元素之间存在多对多的层次关系。</li>
</ul>
</li>
<li>物理结构：数据存储方式。<ul>
<li><strong>顺序存储</strong>：元素连续放置而基于位置访问内存，可随机访问但大小固定和增删效率低。</li>
<li><strong>链式存储</strong>：元素不连续放置而基于指针的引用关系访问内存，大小动态扩展而插入删除效率高，但是访问链表元素时需要循环查找而不能随机访问。</li>
<li><strong>索引存储</strong>：内存整体无序但索引块有序，优化了顺序查找但需要额外空间存储索引表。</li>
<li><strong>散列存储</strong>：数据元素根据函数计算存储位置，基于数据本身查找而查找和存取效率高，但是存取随机而不利于顺序查找，存储位置可能相同而冲突。</li>
</ul>
</li>
</ul>
<h1 id="一、栈"><a href="#一、栈" class="headerlink" title="一、栈"></a>一、栈</h1><blockquote>
<p>遵循先进后出原则的特殊线性表，基于数组或链表实现但只能在栈顶增删元素。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义栈类：底层结构是数组</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Stack</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">items</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span> = items || []</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 入栈</span></span><br><span class="line">    <span class="title function_">push</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">push</span>(element)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 出栈</span></span><br><span class="line">    <span class="title function_">pop</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">pop</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 是否为空栈</span></span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">isEmpty</span>() &#123;</span><br><span class="line">        <span class="keyword">return</span> !<span class="variable language_">this</span>.<span class="property">items</span>.<span class="property">length</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 尺寸</span></span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">size</span>() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">items</span>.<span class="property">length</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 清空栈</span></span><br><span class="line">    <span class="title function_">clear</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span> = []</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 十进制转二进制</span></span><br><span class="line">    toDec2 (dec) &#123;</span><br><span class="line">        <span class="keyword">let</span> res = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">while</span> (dec &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            stack.<span class="title function_">push</span>(dec % <span class="number">2</span>);</span><br><span class="line">            dec = dec &gt;&gt; <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (stack.<span class="property">length</span>) &#123;</span><br><span class="line">            res += stack.<span class="title function_">pop</span>();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用栈类</span></span><br><span class="line"><span class="keyword">const</span> stack = <span class="keyword">new</span> <span class="title class_">Stack</span>()</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(stack.<span class="property">isEmpty</span>)</span><br><span class="line">stack.<span class="title function_">push</span>(<span class="number">5</span>)</span><br></pre></td></tr></table></figure>


<h1 id="二、队列"><a href="#二、队列" class="headerlink" title="二、队列"></a>二、队列</h1><blockquote>
<p>遵循先进先出原则的特殊线性表，只能队尾添加和队首删除，类似排队人群。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Queue</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">items</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span> = items || []</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">enqueue</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">push</span>(element)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 优先队列：根据优先级增删</span></span><br><span class="line">    <span class="title function_">enqueue</span>(<span class="params">element, priority</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> ele = &#123; element, priority &#125;</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">isEmpty</span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">push</span>(ele)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">const</span> preIndex = <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">findIndex</span>(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> item.<span class="property">priority</span> &gt; ele.<span class="property">priority</span></span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="keyword">if</span> (preIndex &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">splice</span>(preIndex, <span class="number">0</span>, ele)</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">push</span>(ele)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">dequeue</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">shift</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">clear</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span> = []</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">size</span>()&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">items</span>.<span class="property">length</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">isEmpty</span>()&#123;</span><br><span class="line">        <span class="keyword">return</span> !<span class="variable language_">this</span>.<span class="property">items</span>.<span class="property">length</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实例化</span></span><br><span class="line"><span class="keyword">const</span> queue = <span class="keyword">new</span> <span class="title class_">Queue</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 普通队列</span></span><br><span class="line">queue.<span class="title function_">enqueue</span>(<span class="string">&#x27;John&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(queue.<span class="property">size</span>)  </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(queue.<span class="property">isEmpty</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 优先队列</span></span><br><span class="line">queue.<span class="title function_">enqueue</span>(<span class="string">&#x27;Surmon&#x27;</span>, <span class="number">3</span>)</span><br><span class="line">queue.<span class="title function_">enqueue</span>(<span class="string">&#x27;John&#x27;</span>, <span class="number">2</span>)</span><br><span class="line">queue.<span class="title function_">enqueue</span>(<span class="string">&#x27;Jack&#x27;</span>, <span class="number">1</span>)</span><br></pre></td></tr></table></figure>


<h1 id="三、链表"><a href="#三、链表" class="headerlink" title="三、链表"></a>三、链表</h1><blockquote>
<p>链接方式存储数据的线性表，是针对顺序存储结构线性表增删效率低问题的优化结果，每个数据单元就是一个包含节点信息和指向后一个节点引用地址的节点。</p>
</blockquote>
<ul>
<li><strong>单链表</strong>：单一的向下传递，每个节点只记录下一个节点的信息。</li>
<li><strong>静态链表</strong>：基于数组实现的链表，每个节点包含了数据和指向地址。</li>
<li><strong>循环链表</strong>：由于单链表到达尾部时回溯到头部很麻烦，所以首尾相连形成循环。</li>
<li><strong>双向链表</strong>：基于单链表增加前指针，每个节点都保存两个分别指向前后节点的地址。</li>
</ul>
  <div align="center">
      <img src="" data-original="/images/js/link-list.png" alt="单链表结构" width="100%">
  </div>

  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 链表节点</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">element</span> = element  <span class="comment">// 数据</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">next</span> = <span class="literal">null</span>        <span class="comment">// 指针</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">prev</span> = <span class="literal">null</span></span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 单链表</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">LinkedList</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">head</span> = <span class="literal">null</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">length</span> = <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 追加元素</span></span><br><span class="line">    <span class="title function_">append</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> node = <span class="keyword">new</span> <span class="title class_">Node</span>(element)</span><br><span class="line">        <span class="keyword">let</span> current = <span class="literal">null</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">head</span> === <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">head</span> = node</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            current = <span class="variable language_">this</span>.<span class="property">head</span></span><br><span class="line">            <span class="keyword">while</span>(current.<span class="property">next</span>) &#123;</span><br><span class="line">                current = current.<span class="property">next</span></span><br><span class="line">            &#125;</span><br><span class="line">            current.<span class="property">next</span> = node</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">length</span>++</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 任意位置插入元素</span></span><br><span class="line">    <span class="title function_">insert</span>(<span class="params">position, element</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &gt;= <span class="number">0</span> &amp;&amp; position &lt;= <span class="variable language_">this</span>.<span class="property">length</span>) &#123;</span><br><span class="line">            <span class="keyword">const</span> node = <span class="keyword">new</span> <span class="title class_">Node</span>(element)</span><br><span class="line">            <span class="keyword">let</span> current = <span class="variable language_">this</span>.<span class="property">head</span></span><br><span class="line">            <span class="keyword">let</span> previous = <span class="literal">null</span></span><br><span class="line">            <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">            <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">head</span> = node</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">while</span> (index++ &lt; position) &#123;</span><br><span class="line">                  previous = current</span><br><span class="line">                  current = current.<span class="property">next</span></span><br><span class="line">                &#125;</span><br><span class="line">                node.<span class="property">next</span> = current</span><br><span class="line">                previous.<span class="property">next</span> = node</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">length</span>++</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 移除指定位置元素</span></span><br><span class="line">    <span class="title function_">removeAt</span>(<span class="params">position</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &gt; -<span class="number">1</span> &amp;&amp; position &lt; length) &#123;</span><br><span class="line">            <span class="keyword">let</span> current = <span class="variable language_">this</span>.<span class="property">head</span></span><br><span class="line">            <span class="keyword">let</span> previous = <span class="literal">null</span></span><br><span class="line">            <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">            <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">head</span> = current.<span class="property">next</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">while</span> (index++ &lt; position) &#123;</span><br><span class="line">                    previous = current</span><br><span class="line">                    current = current.<span class="property">next</span></span><br><span class="line">                &#125;</span><br><span class="line">                previous.<span class="property">next</span> = current.<span class="property">next</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">length</span>--</span><br><span class="line">            <span class="keyword">return</span> current.<span class="property">element</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 寻找元素下标</span></span><br><span class="line">    <span class="title function_">findIndex</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> current = <span class="variable language_">this</span>.<span class="property">head</span></span><br><span class="line">        <span class="keyword">let</span> index = -<span class="number">1</span></span><br><span class="line">        <span class="keyword">while</span> (current) &#123;</span><br><span class="line">            <span class="keyword">if</span> (element === current.<span class="property">element</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> index + <span class="number">1</span></span><br><span class="line">            &#125;</span><br><span class="line">            index++</span><br><span class="line">            current = current.<span class="property">next</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> -<span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 删除指定文档</span></span><br><span class="line">    <span class="title function_">remove</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> index = <span class="variable language_">this</span>.<span class="title function_">indexOf</span>(element)</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">removeAt</span>(index)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">isEmpty</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> !<span class="variable language_">this</span>.<span class="property">length</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">size</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">length</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 转为字符串</span></span><br><span class="line">    <span class="title function_">toString</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> current = <span class="variable language_">this</span>.<span class="property">head</span></span><br><span class="line">        <span class="keyword">let</span> string = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">while</span> (current) &#123;</span><br><span class="line">            string += <span class="string">` <span class="subst">$&#123;current.element&#125;</span>`</span></span><br><span class="line">            current = current.<span class="property">next</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> string</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> linkedList = <span class="keyword">new</span> <span class="title class_">LinkedList</span>()</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(linkedList)</span><br><span class="line">linkedList.<span class="title function_">append</span>(<span class="number">2</span>)</span><br><span class="line">linkedList.<span class="title function_">insert</span>(<span class="number">3</span>, <span class="number">18</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(linkedList)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(linkedList.<span class="title function_">findIndex</span>(<span class="number">24</span>))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 双向链表</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">DoublyLinkedList</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">head</span> = <span class="literal">null</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">tail</span> = <span class="literal">null</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">length</span> = <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 任意位置插入元素</span></span><br><span class="line">    <span class="title function_">insert</span>(<span class="params">position, element</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt; <span class="variable language_">this</span>.<span class="property">length</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">const</span> node = <span class="keyword">new</span> <span class="title class_">Node</span>(element)</span><br><span class="line">        <span class="keyword">let</span> current = <span class="variable language_">this</span>.<span class="property">head</span></span><br><span class="line">        <span class="keyword">let</span> previous = <span class="literal">null</span></span><br><span class="line">        <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;   <span class="comment">// 首位</span></span><br><span class="line">            <span class="keyword">if</span> (!head)&#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">head</span> = node</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">tail</span> = node</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                node.<span class="property">next</span> = current</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">head</span> = node</span><br><span class="line">                current.<span class="property">prev</span> = node</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (position === <span class="variable language_">this</span>.<span class="property">length</span>) &#123;  <span class="comment">// 末位</span></span><br><span class="line">            current = <span class="variable language_">this</span>.<span class="property">tail</span></span><br><span class="line">            current.<span class="property">next</span> = node</span><br><span class="line">            node.<span class="property">prev</span> = current</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">tail</span> = node</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;  <span class="comment">// 中位</span></span><br><span class="line">            <span class="keyword">while</span> (index++ &lt; position) &#123;</span><br><span class="line">              previous = current</span><br><span class="line">              current = current.<span class="property">next</span></span><br><span class="line">            &#125;</span><br><span class="line">            node.<span class="property">next</span> = current</span><br><span class="line">            previous.<span class="property">next</span> = node</span><br><span class="line">            current.<span class="property">prev</span> = node</span><br><span class="line">            node.<span class="property">prev</span> = previous</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">length</span>++</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 移除指定位置元素</span></span><br><span class="line">    <span class="title function_">removeAt</span>(<span class="params">position</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt; <span class="variable language_">this</span>.<span class="property">length</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> current = <span class="variable language_">this</span>.<span class="property">head</span></span><br><span class="line">        <span class="keyword">let</span> previous = <span class="literal">null</span></span><br><span class="line">        <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;  <span class="comment">// 首位</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">head</span> = <span class="variable language_">this</span>.<span class="property">head</span>.<span class="property">next</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">head</span>.<span class="property">prev</span> = <span class="literal">null</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">length</span> === <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">tail</span> = <span class="literal">null</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (position === <span class="variable language_">this</span>.<span class="property">length</span> - <span class="number">1</span>) &#123;  <span class="comment">// 末位</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">tail</span> = <span class="variable language_">this</span>.<span class="property">tail</span>.<span class="property">prev</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">tail</span>.<span class="property">next</span> = <span class="literal">null</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;  <span class="comment">// 中位</span></span><br><span class="line">            <span class="keyword">while</span> (index++ &lt; position) &#123;</span><br><span class="line">              previous = current</span><br><span class="line">              current = current.<span class="property">next</span></span><br><span class="line">            &#125;</span><br><span class="line">            previous.<span class="property">next</span> = current.<span class="property">next</span></span><br><span class="line">            current.<span class="property">next</span>.<span class="property">prev</span> = previous</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">length</span>--</span><br><span class="line">        <span class="keyword">return</span> current.<span class="property">element</span> </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="四、集合"><a href="#四、集合" class="headerlink" title="四、集合"></a>四、集合</h1>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Set</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span> = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">has</span>(<span class="params">value</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">hasOwnProperty</span>(value)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">add</span>(<span class="params">value</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">has</span>(value)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span> </span><br><span class="line">        &#125; </span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span>[value] = value</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>    </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">remove</span>(<span class="params">value</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="title function_">has</span>(value)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span> </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">delete</span> <span class="variable language_">this</span>.<span class="property">items</span>[value]</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">size</span>() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="title function_">keys</span>(<span class="variable language_">this</span>.<span class="property">items</span>).<span class="property">length</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">values</span>() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="title function_">keys</span>(<span class="variable language_">this</span>.<span class="property">items</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">union</span>(<span class="params">otherSet</span>) &#123;  <span class="comment">// 并集</span></span><br><span class="line">        <span class="keyword">const</span> unionSet = <span class="keyword">new</span> <span class="title class_">Set</span>()</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">values</span>.<span class="title function_">forEach</span>(<span class="function"><span class="params">val</span> =&gt;</span> unionSet.<span class="title function_">add</span>(val))</span><br><span class="line">        otherSet.<span class="property">values</span>.<span class="title function_">forEach</span>(<span class="function"><span class="params">val</span> =&gt;</span> unionSet.<span class="title function_">add</span>(val))</span><br><span class="line">        <span class="keyword">return</span> unionSet</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">intersection</span>(<span class="params">otherSet</span>) &#123;  <span class="comment">// 交集</span></span><br><span class="line">        <span class="keyword">const</span> intersectionSet = <span class="keyword">new</span> <span class="title class_">Set</span>()</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">values</span>.<span class="title function_">forEach</span>(<span class="function"><span class="params">val</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (otherSet.<span class="title function_">has</span>(val)) &#123;</span><br><span class="line">                intersectionSet.<span class="title function_">add</span>(val)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> intersectionSet</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">difference</span>(<span class="params">otherSet</span>) &#123;  <span class="comment">// 差集</span></span><br><span class="line">        <span class="keyword">const</span> differenceSet = <span class="keyword">new</span> <span class="title class_">Set</span>()</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">values</span>.<span class="title function_">forEach</span>(<span class="function"><span class="params">val</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (!otherSet.<span class="title function_">has</span>(val)) &#123;</span><br><span class="line">                differenceSet.<span class="title function_">add</span>(val)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> differenceSet</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">subset</span>(<span class="params">otherSet</span>) &#123;  <span class="comment">// 子集</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">size</span> &gt; otherSet.<span class="property">size</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> !<span class="variable language_">this</span>.<span class="property">values</span>.<span class="title function_">some</span>(<span class="function"><span class="params">v</span> =&gt;</span> !otherSet.<span class="title function_">has</span>(v))</span><br><span class="line">        &#125; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> set1 = <span class="keyword">new</span> <span class="title class_">Set</span>();</span><br><span class="line">set1.<span class="title function_">add</span>(<span class="number">1</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set1.<span class="property">values</span>, set1.<span class="title function_">has</span>(<span class="number">1</span>))   </span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> set2 = <span class="keyword">new</span> <span class="title class_">Set</span>();</span><br><span class="line">set2.<span class="title function_">add</span>(<span class="number">1</span>);</span><br><span class="line">set2.<span class="title function_">add</span>(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">let</span> set11 = set1.<span class="title function_">union</span>(set2);</span><br></pre></td></tr></table></figure>


<h1 id="五、字典"><a href="#五、字典" class="headerlink" title="五、字典"></a>五、字典</h1><blockquote>
<p>以键值对的形式存储不重复的数据，Object 对象就是字典在 Js 中的实现。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Dictionary</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span> = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">set</span>(<span class="params">key, value</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">items</span>[key] = value</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">items</span>[key]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">remove</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">delete</span> <span class="variable language_">this</span>.<span class="property">items</span>[key]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">keys</span>() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="title function_">keys</span>(<span class="variable language_">this</span>.<span class="property">items</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">values</span>() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="title function_">values</span>(<span class="variable language_">this</span>.<span class="property">items</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> dict = <span class="keyword">new</span> <span class="title class_">Dictionary</span>()</span><br><span class="line">dict.<span class="title function_">set</span>(<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;John&#x27;</span>)</span><br><span class="line">dict.<span class="title function_">set</span>(<span class="string">&#x27;age&#x27;</span>, <span class="number">30</span>) </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(dict.<span class="property">keys</span>) </span><br></pre></td></tr></table></figure>


<h1 id="六、散列表"><a href="#六、散列表" class="headerlink" title="六、散列表"></a>六、散列表</h1><blockquote>
<p>又称哈希表，是通过关键字将数据映射到一个内存位置而生成的一种便于搜索的数据结构。散列函数就是通过<code>数字分析法、平方取中法、分段叠加法、除留余数法</code>等实现的映射函数，其原则为：函数本身便于计算、计算出来的地址分布均匀。注意键值不同的元素可能会映射到散列表的同一地址造成 <code>hash 冲突</code>，处理冲突的方式主要如下：</p>
</blockquote>
<ul>
<li><strong>再散列法</strong>：冲突时通过其它散列函数计算地址。</li>
<li><strong>建立公共溢出区</strong>：将所有和散列表冲突的元素都放入溢出表。</li>
<li><strong>拉链法&#x2F;分离链接</strong>：将所有散列地址相同的记录都放入一个线性链表。</li>
<li><strong>开放定址法</strong>：冲突时寻找散列表中的空地址。线性探测 (顺序查看表中下一单元)、二次探测 (在表的左右进行跳跃式探测)、随机探测 (建立一个伪随机数发生器)。</li>
</ul>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">HashTable</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">table</span> = []</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 散列函数</span></span><br><span class="line">    <span class="keyword">static</span> <span class="title function_">loseloseHashCode</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> hash = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> codePoint <span class="keyword">of</span> key) &#123;</span><br><span class="line">            hash += codePoint.<span class="title function_">charCodeAt</span>()</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> hash % <span class="number">37</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 不考虑冲突</span></span><br><span class="line">    <span class="title function_">put</span>(<span class="params">key, value</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> position = <span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`<span class="subst">$&#123;position&#125;</span> - <span class="subst">$&#123;key&#125;</span>`</span>)</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">table</span>[position] = value</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">table</span>[<span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">remove</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">table</span>[<span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)] = <span class="literal">undefined</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 分离链接处理冲突：需要一个辅助类 LinkedList 表示要加入的元素</span></span><br><span class="line">    <span class="title function_">put</span>(<span class="params">key, value</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> position = <span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">table</span>[position] === <span class="literal">undefined</span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">table</span>[position] = <span class="keyword">new</span> <span class="title class_">LinkedList</span>()</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">table</span>[position].<span class="title function_">append</span>(&#123; key, value &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> position = <span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">table</span>[position] === <span class="literal">undefined</span>) <span class="keyword">return</span> <span class="literal">undefined</span></span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">getElementValue</span> = node =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (!node &amp;&amp; !node.<span class="property">element</span>) <span class="keyword">return</span> <span class="literal">undefined</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="title class_">Object</span>.<span class="title function_">is</span>(node.<span class="property">element</span>.<span class="property">key</span>, key)) &#123;</span><br><span class="line">                <span class="keyword">return</span> node.<span class="property">element</span>.<span class="property">value</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="title function_">getElementValue</span>(node.<span class="property">next</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">getElementValue</span>(<span class="variable language_">this</span>.<span class="property">table</span>[position].<span class="property">head</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">remove</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> position = <span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">table</span>[position] === <span class="literal">undefined</span>) <span class="keyword">return</span> <span class="literal">undefined</span></span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">getElementValue</span> = node =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (!node &amp;&amp; !node.<span class="property">element</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">            <span class="keyword">if</span> (!<span class="title class_">Object</span>.<span class="title function_">is</span>(node.<span class="property">element</span>.<span class="property">key</span>, key)) &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">table</span>[position].<span class="title function_">remove</span>(node.<span class="property">element</span>)</span><br><span class="line">                <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">table</span>[position].<span class="property">isEmpty</span>) &#123;</span><br><span class="line">                    <span class="variable language_">this</span>.<span class="property">table</span>[position] = <span class="literal">undefined</span></span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="title function_">getElementValue</span>(node.<span class="property">next</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">getElementValue</span>(<span class="variable language_">this</span>.<span class="property">table</span>[position].<span class="property">head</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 线性探查</span></span><br><span class="line">    <span class="title function_">put</span>(<span class="params">key, value</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> position = <span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">table</span>[position] === <span class="literal">undefined</span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">table</span>[position] = &#123; key, value &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> index = ++position</span><br><span class="line">            <span class="keyword">while</span> (<span class="variable language_">this</span>.<span class="property">table</span>[index] !== <span class="literal">undefined</span>) &#123;</span><br><span class="line">                index++</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">table</span>[index] = &#123; key, value &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">table</span>[position].<span class="title function_">append</span>(&#123; key, value &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> position = <span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">getElementValue</span> = index =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">table</span>[index] === <span class="literal">undefined</span>) <span class="keyword">return</span> <span class="literal">undefined</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="title class_">Object</span>.<span class="title function_">is</span>(<span class="variable language_">this</span>.<span class="property">table</span>[index].<span class="property">key</span>, key)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">table</span>[index].<span class="property">value</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="title function_">getElementValue</span>(index + <span class="number">1</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">getElementValue</span>(position)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">remove</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> position = <span class="title class_">HashTable</span>.<span class="title function_">loseloseHashCode</span>(key)</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">removeElementValue</span> = index =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">table</span>[index] === <span class="literal">undefined</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="title class_">Object</span>.<span class="title function_">is</span>(<span class="variable language_">this</span>.<span class="property">table</span>[index].<span class="property">key</span>, key)) &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">table</span>[index] = <span class="literal">undefined</span></span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="title function_">removeElementValue</span>(index + <span class="number">1</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">removeElementValue</span>(position)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> hash = <span class="keyword">new</span> <span class="title class_">HashTable</span>()</span><br><span class="line">hash.<span class="title function_">put</span>(<span class="string">&#x27;Surm&#x27;</span>, <span class="string">&#x27;surm@email.com&#x27;</span>)   <span class="comment">// 19 - Surm</span></span><br><span class="line">hash.<span class="title function_">put</span>(<span class="string">&#x27;John&#x27;</span>, <span class="string">&#x27;john@email.com&#x27;</span>)   <span class="comment">// 29 - John</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(hash.<span class="title function_">get</span>(<span class="string">&#x27;Surmon&#x27;</span>))      <span class="comment">// surm@email.com</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(hash.<span class="title function_">get</span>(<span class="string">&#x27;Loiane&#x27;</span>))      <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure>


<h1 id="七、树"><a href="#七、树" class="headerlink" title="七、树"></a>七、树</h1><blockquote>
<p>相同类型元素的有限集合、一对多的非线性结构、分层数据的抽象模型。主要属性有：<code>节点</code> (没有父节点的根节点、有子节点的分支节点、没有子节点的叶子节点)、<code>子树</code> (节点组成的树)、<code>层级</code> (根据节点级别分层)、<code>深度</code> (到根节点的节点数量)、<code>高度</code> (节点深度中的最大值)、<code>权值</code>。复杂的树状结构都需要转换为二叉树处理，常用类型如下：</p>
</blockquote>
<ul>
<li><strong>二叉树</strong>：每个节点最多含有两个子树，子节点之间有顺序关系。<ul>
<li>完全二叉树：最后一层右边不定，左边和其余层都是满的，比如堆。</li>
<li>满二叉树：所有叶节点都在最底层的完全二叉树。</li>
<li>排序二叉树：有序二叉树、二叉查找树、二叉搜索树。</li>
<li>平衡二叉树：AVL树，任何节点的左右子树的高度差不超过 1。</li>
</ul>
</li>
<li><strong>哈夫曼树</strong>：最优二叉树，带权路径最短的二叉树。<ul>
<li>路径长度：两个节点之间所经过的分支数目。</li>
<li>树带权路径长度：所有叶子节点的带权路径长度之和。</li>
<li>节点带权路径长度：节点到根节点的分支数目和该节点权值的乘积。</li>
</ul>
</li>
<li><strong>B 树</strong>：一种优化读写操作的二叉查找树，保持数据有序并有多余两个子树。</li>
</ul>
  <div align="center">  
      <img src="" data-original="/images/js/binary-order.png" alt="二叉树顺序存储结构" width="100%">
  </div>
  <div align="center">
      <img src="" data-original="/images/js/binary-link.png" alt="二叉树链式存储结构" width="100%">
  </div>


  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 顺序存储</span></span><br><span class="line"><span class="keyword">var</span> tree = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, , <span class="number">6</span>, , , <span class="number">7</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 链式存储</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">key, left = <span class="literal">null</span>, right = <span class="literal">null</span></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">key</span> = key</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">left</span> = left</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">right</span> = right</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 二叉树类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">BinarySearchTree</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">root</span> = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">insert</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> newNode = <span class="keyword">new</span> <span class="title class_">Node</span>(key)</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">insertNode</span> = (<span class="params">node, newNode</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (newNode.<span class="property">key</span> &lt; node.<span class="property">key</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (node.<span class="property">left</span> === <span class="literal">null</span>) &#123;</span><br><span class="line">                    node.<span class="property">left</span> = newNode</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="title function_">insertNode</span>(node.<span class="property">left</span>, newNode)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (node.<span class="property">right</span> === <span class="literal">null</span>) &#123;</span><br><span class="line">                    node.<span class="property">right</span> = newNode</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="title function_">insertNode</span>(node.<span class="property">right</span>, newNode)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="property">root</span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">root</span> = newNode</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="title function_">insertNode</span>(<span class="variable language_">this</span>.<span class="property">root</span>, newNode)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 中序遍历：左侧子节点、节点本身、右侧子节点，可用于排序操作。</span></span><br><span class="line">    <span class="title function_">inOrderTraverse</span>(<span class="params">callback</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">inOrderTraverseNode</span> = (<span class="params">node, callback</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (node !== <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="title function_">inOrderTraverseNode</span>(node.<span class="property">left</span>, callback)</span><br><span class="line">                <span class="title function_">callback</span>(node.<span class="property">key</span>)</span><br><span class="line">                <span class="title function_">inOrderTraverseNode</span>(node.<span class="property">right</span>, callback)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">inOrderTraverseNode</span>(<span class="variable language_">this</span>.<span class="property">root</span>, callback)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 前序遍历：节点本身、左侧子节点、右侧子节点，可用于打印结构化的文档</span></span><br><span class="line">    <span class="title function_">preOrderTraverse</span>(<span class="params">callback</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">preOrderTraverseNode</span> = (<span class="params">node, callback</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (node !== <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="title function_">callback</span>(node.<span class="property">key</span>)</span><br><span class="line">                <span class="title function_">preOrderTraverseNode</span>(node.<span class="property">left</span>, callback)</span><br><span class="line">                <span class="title function_">preOrderTraverseNode</span>(node.<span class="property">right</span>, callback)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">preOrderTraverseNode</span>(<span class="variable language_">this</span>.<span class="property">root</span>, callback)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 后序遍历：左侧子节点、节点本身、右侧子节点，可用于计算目录下的文件空间</span></span><br><span class="line">    <span class="title function_">postOrderTraverse</span>(<span class="params">callback</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">postOrderTraverseNode</span> = (<span class="params">node, callback</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (node !== <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="title function_">postOrderTraverseNode</span>(node.<span class="property">left</span>, callback)</span><br><span class="line">                <span class="title function_">postOrderTraverseNode</span>(node.<span class="property">right</span>, callback)</span><br><span class="line">                <span class="title function_">callback</span>(node.<span class="property">key</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">postOrderTraverseNode</span>(<span class="variable language_">this</span>.<span class="property">root</span>, callback)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">min</span>(<span class="params">node</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">minNode</span> = node =&gt; &#123;</span><br><span class="line">          <span class="keyword">return</span> node ? (node.<span class="property">left</span> ? <span class="title function_">minNode</span>(node.<span class="property">left</span>) : node) : <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">minNode</span>(node || <span class="variable language_">this</span>.<span class="property">root</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">max</span>(<span class="params">node</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">maxNode</span> = node =&gt; &#123;</span><br><span class="line">          <span class="keyword">return</span> node ? (node.<span class="property">right</span> ? <span class="title function_">maxNode</span>(node.<span class="property">right</span>) : node) : <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">maxNode</span>(node || <span class="variable language_">this</span>.<span class="property">root</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">search</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">searchNode</span> = (<span class="params">node, key</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (node === <span class="literal">null</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">            <span class="keyword">if</span> (node.<span class="property">key</span> === key) <span class="keyword">return</span> node</span><br><span class="line">            <span class="keyword">let</span> _node = node.<span class="property">key</span> &gt; key ? node.<span class="property">left</span> : node.<span class="property">right</span></span><br><span class="line">            <span class="keyword">return</span> <span class="title function_">searchNode</span>(_node, key)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="title function_">searchNode</span>(root, key)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">remove</span>(<span class="params">key</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">_removeNode</span>(<span class="variable language_">this</span>.<span class="property">root</span>, key)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">_removeNode</span>(<span class="params">node, key</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (node === <span class="literal">null</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (key &lt; node.<span class="property">key</span>) &#123;</span><br><span class="line">            node.<span class="property">left</span> = <span class="variable language_">this</span>.<span class="title function_">_removeNode</span>(node.<span class="property">left</span>, key)</span><br><span class="line">            <span class="keyword">return</span> node</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (key &gt; node.<span class="property">key</span>) &#123;</span><br><span class="line">            node.<span class="property">right</span> = <span class="variable language_">this</span>.<span class="title function_">_removeNode</span>(node.<span class="property">right</span>, key)</span><br><span class="line">            <span class="keyword">return</span> node</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (node.<span class="property">left</span> === <span class="literal">null</span> &amp;&amp; node.<span class="property">right</span> === <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (node.<span class="property">left</span> === <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> node.<span class="property">right</span></span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (node.<span class="property">right</span> === <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> node.<span class="property">left</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">const</span> minNode = <span class="variable language_">this</span>.<span class="title function_">_findMinNode</span>(node.<span class="property">right</span>)</span><br><span class="line">                node.<span class="property">key</span> = minNode.<span class="property">key</span></span><br><span class="line">                node.<span class="property">right</span> = <span class="variable language_">this</span>.<span class="title function_">_removeNode</span>(node.<span class="property">right</span>, minNode.<span class="property">key</span>)</span><br><span class="line">                <span class="keyword">return</span> node</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> tree = <span class="keyword">new</span> <span class="title class_">BinarySearchTree</span>()</span><br><span class="line">tree.<span class="title function_">insert</span>(<span class="number">11</span>)</span><br><span class="line">tree.<span class="title function_">insert</span>(<span class="number">7</span>)</span><br><span class="line">tree.<span class="title function_">insert</span>(<span class="number">5</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(tree)</span><br><span class="line">tree.<span class="title function_">inOrderTraverse</span>(<span class="function"><span class="params">value</span> =&gt;</span> &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(value) &#125;)</span><br></pre></td></tr></table></figure>


<h1 id="八、图"><a href="#八、图" class="headerlink" title="八、图"></a>八、图</h1><blockquote>
<p>由一系列顶点和连接顶点的边组成的一种非线性数据结构，可以用来表示任何二元关系，采用哪种方式取决于图的类型和待解决的问题。</p>
</blockquote>
<ul>
<li><strong>邻接矩阵</strong>：通过一个二维数组表示顶点连接，但可能浪费内存。</li>
<li><strong>邻接表</strong>：由每个顶点的相邻顶点列表组成的一对多结构，比如数组、链表、散列表、字典。</li>
<li><strong>关联矩阵</strong>：矩阵的行列分别表示顶点和边并通过二维数组表示连接，常用于边的数量比顶点少时节省空间和内存。如果顶点 A 是边 E 的入射点，则 array[A][E] &#x3D;&#x3D;&#x3D; 1，否则为 0。</li>
</ul>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Graph</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">vertices</span> = []</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">adjList</span> = <span class="keyword">new</span> <span class="title class_">Dictionary</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 添加顶点</span></span><br><span class="line">    <span class="title function_">addVertex</span>(<span class="params">v</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">vertices</span>.<span class="title function_">push</span>(v)</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">adjList</span>.<span class="title function_">set</span>(v, [])</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 添加线</span></span><br><span class="line">    <span class="title function_">addEdge</span>(<span class="params">v, w</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">adjList</span>.<span class="title function_">get</span>(v).<span class="title function_">push</span>(w)</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">adjList</span>.<span class="title function_">get</span>(w).<span class="title function_">push</span>(v)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 转字符串</span></span><br><span class="line">    <span class="title function_">toString</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">vertices</span>.<span class="title function_">reduce</span>(<span class="function">(<span class="params">r, v, i</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">adjList</span>.<span class="title function_">get</span>(v).<span class="title function_">reduce</span>(<span class="function">(<span class="params">r, w, i</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> r + <span class="string">`<span class="subst">$&#123;w&#125;</span> `</span></span><br><span class="line">            &#125;, <span class="string">`<span class="subst">$&#123;r&#125;</span>\n<span class="subst">$&#123;v&#125;</span> =&gt; `</span>)</span><br><span class="line">        &#125;, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 构建顶点之间的路径。</span></span><br><span class="line">    <span class="title function_">distance</span>(<span class="params">fromVertex</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> vertices = <span class="variable language_">this</span>.<span class="property">vertices</span></span><br><span class="line">        <span class="keyword">const</span> &#123; distances, predecessors &#125; = <span class="variable language_">this</span>.<span class="title function_">bfs</span>(fromVertex)</span><br><span class="line">        vertices.<span class="title function_">forEach</span>(<span class="function"><span class="params">toVertex</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (!!distances[toVertex]) &#123;</span><br><span class="line">                <span class="keyword">let</span> preVertex = predecessors[toVertex]</span><br><span class="line">                <span class="keyword">let</span> slug = <span class="string">&#x27;&#x27;</span></span><br><span class="line">                <span class="keyword">while</span> (fromVertex !== preVertex) &#123;</span><br><span class="line">                    slug = <span class="string">`<span class="subst">$&#123;preVertex&#125;</span> - <span class="subst">$&#123;slug&#125;</span>`</span></span><br><span class="line">                    preVertex = predecessors[preVertex]</span><br><span class="line">                &#125;</span><br><span class="line">                slug = <span class="string">`<span class="subst">$&#123;fromVertex&#125;</span> - <span class="subst">$&#123;slug&#125;</span><span class="subst">$&#123;toVertex&#125;</span>`</span></span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(slug)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 广度优先搜索算法：一层一层的遍历顶点。</span></span><br><span class="line">    <span class="title function_">bfs</span>(<span class="params">v, callback</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> read = []</span><br><span class="line">        <span class="keyword">const</span> distances = []</span><br><span class="line">        <span class="keyword">const</span> predecessors = []</span><br><span class="line">        <span class="keyword">const</span> adjList = <span class="variable language_">this</span>.<span class="property">adjList</span></span><br><span class="line">        <span class="keyword">const</span> pending = [v || <span class="variable language_">this</span>.<span class="property">vertices</span>[<span class="number">0</span>]]</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">readVertices</span> = vertices =&gt; &#123;</span><br><span class="line">            vertices.<span class="title function_">forEach</span>(<span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">                read.<span class="title function_">push</span>(key)</span><br><span class="line">                pending.<span class="title function_">shift</span>()</span><br><span class="line">                distances[key] = distances[key] || <span class="number">0</span></span><br><span class="line">                predecessors[key] = predecessors[key] || <span class="literal">null</span></span><br><span class="line">                adjList.<span class="title function_">get</span>(key).<span class="title function_">forEach</span>(<span class="function"><span class="params">v</span> =&gt;</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (!pending.<span class="title function_">includes</span>(v) &amp;&amp; !read.<span class="title function_">includes</span>(v)) &#123;</span><br><span class="line">                        pending.<span class="title function_">push</span>(v)</span><br><span class="line">                        distances[v] = distances[key] + <span class="number">1</span></span><br><span class="line">                        predecessors[v] = key</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">                <span class="keyword">if</span> (callback) <span class="title function_">callback</span>(key)</span><br><span class="line">                <span class="keyword">if</span> (pending.<span class="property">length</span>) <span class="title function_">readVertices</span>(pending)</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">readVertices</span>(pending)</span><br><span class="line">        <span class="keyword">return</span> &#123; distances, predecessors &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 深度优先搜索算法：选择一个路径遍历到头，然后递归回去选择未访问的顶点。</span></span><br><span class="line">    <span class="title function_">dfs</span>(<span class="params">callback</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> readTimer = <span class="number">0</span></span><br><span class="line">        <span class="keyword">const</span> read = []</span><br><span class="line">        <span class="keyword">const</span> readTimes = []</span><br><span class="line">        <span class="keyword">const</span> finishedTimes = []</span><br><span class="line">        <span class="keyword">const</span> predecessors = []</span><br><span class="line">        <span class="keyword">const</span> adjList = <span class="variable language_">this</span>.<span class="property">adjList</span></span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">readVertices</span> = (<span class="params">vertices, predecessor = <span class="literal">null</span></span>) =&gt; &#123;</span><br><span class="line">            vertices.<span class="title function_">forEach</span>(<span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">                readTimer++</span><br><span class="line">                <span class="keyword">if</span> (adjList.<span class="title function_">get</span>(key).<span class="title function_">every</span>(<span class="function"><span class="params">v</span> =&gt;</span> read.<span class="title function_">includes</span>(v)) </span><br><span class="line">                &amp;&amp; !finishedTimes[key]) &#123;</span><br><span class="line">                    finishedTimes[key] = readTimer</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (read.<span class="title function_">includes</span>(key)) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">                readTimes[key] = readTimer</span><br><span class="line">                read.<span class="title function_">push</span>(key)</span><br><span class="line">                <span class="keyword">if</span> (callback) <span class="title function_">callback</span>(key)</span><br><span class="line">                predecessors[key] = predecessors[key] || predecessor</span><br><span class="line">                <span class="keyword">if</span> (read.<span class="property">length</span> !== <span class="variable language_">this</span>.<span class="property">vertices</span>.<span class="property">length</span>) &#123;</span><br><span class="line">                    <span class="title function_">readVertices</span>(adjList.<span class="title function_">get</span>(key), key)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">readVertices</span>(adjList.<span class="property">keys</span>)</span><br><span class="line">        <span class="keyword">return</span> &#123; readTimes, finishedTimes, predecessors &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> graph = <span class="keyword">new</span> <span class="title class_">Graph</span>()</span><br><span class="line"><span class="keyword">const</span> vertices = [<span class="string">&#x27;A&#x27;</span>,<span class="string">&#x27;B&#x27;</span>,<span class="string">&#x27;C&#x27;</span>,<span class="string">&#x27;D&#x27;</span>];</span><br><span class="line">vertices.<span class="title function_">forEach</span>(<span class="function"><span class="params">v</span> =&gt;</span> graph.<span class="title function_">addVertex</span>(v))</span><br><span class="line"></span><br><span class="line">graph.<span class="title function_">addEdge</span>(<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>)</span><br><span class="line">graph.<span class="title function_">addEdge</span>(<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;C&#x27;</span>)</span><br><span class="line">graph.<span class="title function_">addEdge</span>(<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;D&#x27;</span>)</span><br><span class="line">graph.<span class="title function_">addEdge</span>(<span class="string">&#x27;C&#x27;</span>, <span class="string">&#x27;D&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(graph.<span class="title function_">toString</span>())  </span><br><span class="line">graph.<span class="title function_">bfs</span>(graph.<span class="property">vertices</span>[<span class="number">0</span>], <span class="function"><span class="params">val</span> =&gt;</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(val))</span><br></pre></td></tr></table></figure>




    </div>

    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;margin-top:20px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>chuangges
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chuangges.gitee.io/2019/05/20/4-4-js%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" title="JS 数据结构">https://chuangges.gitee.io/2019/05/20/4-4-js数据结构/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh_CN" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/05/18/4-3-js%E6%95%B0%E6%8D%AE/" rel="prev" title="JS 数据类型">
      <i class="fa fa-chevron-left"></i> JS 数据类型
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/05/23/4-5-ES%E8%AF%AD%E6%B3%95/" rel="next" title="ES6 ～ ES9 新特性">
      ES6 ～ ES9 新特性 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%A0%88"><span class="nav-number">1.</span> <span class="nav-text">一、栈</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E9%98%9F%E5%88%97"><span class="nav-number">2.</span> <span class="nav-text">二、队列</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%93%BE%E8%A1%A8"><span class="nav-number">3.</span> <span class="nav-text">三、链表</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E9%9B%86%E5%90%88"><span class="nav-number">4.</span> <span class="nav-text">四、集合</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E5%AD%97%E5%85%B8"><span class="nav-number">5.</span> <span class="nav-text">五、字典</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E6%95%A3%E5%88%97%E8%A1%A8"><span class="nav-number">6.</span> <span class="nav-text">六、散列表</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81%E6%A0%91"><span class="nav-number">7.</span> <span class="nav-text">七、树</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AB%E3%80%81%E5%9B%BE"><span class="nav-number">8.</span> <span class="nav-text">八、图</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="chuangges" src="" data-original="/images/chuang.png">
  <p class="site-author-name" itemprop="name">chuangges</p>
  <div class="site-description" itemprop="description">chuangge's blog.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chuangges" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chuangges" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://juejin.im/" title="https:&#x2F;&#x2F;juejin.im&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">掘金</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.nowcoder.com/" title="https:&#x2F;&#x2F;www.nowcoder.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">牛客网</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.docschina.org/" title="https:&#x2F;&#x2F;www.docschina.org&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">印记中文</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://tool.lu/" title="http:&#x2F;&#x2F;tool.lu&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">在线工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tinypng.com/" title="https:&#x2F;&#x2F;tinypng.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片压缩</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://bigjpg.com/" title="https:&#x2F;&#x2F;bigjpg.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片放大</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tool.chinaz.com/" title="https:&#x2F;&#x2F;tool.chinaz.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">站长工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://learn-anything.xyz/" title="https:&#x2F;&#x2F;learn-anything.xyz&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Learn-Anything</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Stack-Overflow</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chuangge</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">1m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">15:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"left","width":180,"height":360},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script>
        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(e){e.imageLazyLoadSetting.processImages=t;var n=e.imageLazyLoadSetting.isSPA,i=e.imageLazyLoadSetting.preloadRatio||1,r=o();function o(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){n&&(r=o());for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(e.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];e=function(){r=r.filter(function(t){return o!==t})},(t=o).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,t.removeAttribute("data-original"),e&&e()},t.src!==i&&(n.src=i))}()}function a(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",a),e.addEventListener("resize",a),e.addEventListener("orientationchange",a)}(this);</script></body>
</html>
